package com.css.dzj.zfjg.modules.operatingabnormal.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.css.dzj.zfjg.modules.operatingabnormal.entity.JgLhjgOperatingAbnormalEntity;
import com.css.dzj.zfjg.modules.operatingabnormal.entity.QJgLhjgOperatingAbnormalEntity;
import com.css.dzj.zfjg.modules.operatingabnormal.mapper.JgLhjgOperatingAbnormallMapper;
import com.css.dzj.zfjg.modules.operatingabnormal.service.JgLhjgOperatingAbnormalService;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * 企业经营异常名录信息Service实现
 *
 * @author liyuan
 */
@Service
public class JgLhjgOperatingAbnormalServiceImpl implements JgLhjgOperatingAbnormalService {
    @Autowired
    private JgLhjgOperatingAbnormallMapper abnormallMapper;
    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Override
    public QueryResults<JgLhjgOperatingAbnormalEntity> queryPage(String enterpriseName, Pageable pageable) {
        QJgLhjgOperatingAbnormalEntity q = QJgLhjgOperatingAbnormalEntity.jgLhjgOperatingAbnormalEntity;
        Predicate predicate = q.isNotNull().or(q.isNull());
        /**
         * 查询条件
         */
        predicate = (StringUtils.isEmpty(enterpriseName)) ? predicate : ExpressionUtils.and(predicate, q.enterpriseName.like("%" + enterpriseName + "%"));

        // 返回结果
        return jpaQueryFactory.selectFrom(q).where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
    }

    @Override
    public JgLhjgOperatingAbnormalEntity findByTaskNo(String taskNo) {
        QJgLhjgOperatingAbnormalEntity q = QJgLhjgOperatingAbnormalEntity.jgLhjgOperatingAbnormalEntity;
        return jpaQueryFactory.selectFrom(q).where(q.taskNo.eq(taskNo)).fetchOne();
    }

    @Override
    public JgLhjgOperatingAbnormalEntity getById(String id) {
        return abnormallMapper.getOne(id);
    }

    @Override
    public void save(JgLhjgOperatingAbnormalEntity abnormalEntity) {
        if (abnormalEntity == null){
            throw new RuntimeException("实体为空");
        }
        abnormallMapper.save(abnormalEntity);
    }

    @Override
    public void updateById(JgLhjgOperatingAbnormalEntity abnormalEntity) {
        if (abnormalEntity == null){
            throw new RuntimeException("实体为空");
        }
        if (StringUtils.isEmpty(abnormalEntity.getOperatingAbnormalId())){
            // 实体中必须有ID
            throw new RuntimeException("实体ID为空");
        }
        abnormallMapper.save(abnormalEntity);
    }

    @Override
    public void removeByIds(List<String> ids) {
        QJgLhjgOperatingAbnormalEntity q = QJgLhjgOperatingAbnormalEntity.jgLhjgOperatingAbnormalEntity;
        jpaQueryFactory.delete(q).where(q.operatingAbnormalId.in(ids)).execute();
    }
}
